package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsUser;
import com.qf.pojo.DtsUserAccount;
import com.qf.service.DtsAdminService;
import com.qf.service.DtsUserService;
import com.qf.service.DtsUserServiceImpl;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin/user")
public class AdminUserController {
    //注入service
    @Autowired
    private DtsUserService dtsUserService;

    /**
     * admin/user/list?page=1&limit=20&sort=add_time&order=desc
     * page: 1
     * limit: 20
     * sort: add_time
     * order: desc
     */
    @GetMapping("/list")
    public Object userlist(int page,int limit,String sort,String order,String username,String mobile){
        //获得会员集合--指定范围，页数以及每一页多少条
        List<DtsUser> users = dtsUserService.findUser(page,limit,sort,order,username,mobile);
        //total总数
        Integer count = dtsUserService.userCount();
        Map<String,Object> result1 =new HashMap<>();

        result1.put("total",count);
        result1.put("items",users);
        return ResponseUtil.ok(result1);


    }
    //会员审批
    @RequestMapping("/approveAgency")
    public Object approveAgency(@RequestBody String body){
        Integer userId = JacksonUtil.parseInteger(body, "userId");
        Integer settlementRate = JacksonUtil.parseInteger(body, "settlementRate");

        dtsUserService.approveAgency(userId,settlementRate);
        return ResponseUtil.ok();
    }
    //会员推广代理
    @GetMapping("/detailApprove")
    public Object detailApprove(int id){
        DtsUserAccount dtsUserAccount = dtsUserService.detailApprove(id);
        Map<String,Object> result =new HashMap<>();
        result.put("id",dtsUserAccount.getId());
        result.put("userId",dtsUserAccount.getUserId());
        result.put("remainAmount",dtsUserAccount.getRemainAmount());
        result.put("totalAmount",dtsUserAccount.getTotalAmount());
        result.put("createTime",dtsUserAccount.getCreateTime());
        result.put("modifyTime",dtsUserAccount.getModifyTime());
        result.put("settlementRate",dtsUserAccount.getSettlementRate());
        result.put("status",dtsUserAccount.getStatus());
        return ResponseUtil.ok(result);
    }


}
